package com.carwash.controller;

import com.carwash.model.ChatRequest;
import com.carwash.model.ChatResponse;
import com.carwash.service.DeepSeekService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api")
@CrossOrigin
@Slf4j
public class ChatController {
    private final DeepSeekService deepSeekService;
    
    public ChatController(DeepSeekService deepSeekService) {
        this.deepSeekService = deepSeekService;
    }
    
    @PostMapping("/chat")
    public ResponseEntity<ChatResponse> chat(@RequestBody ChatRequest request) {
        try {
            String answer = deepSeekService.chat(request.getText());
            return ResponseEntity.ok(new ChatResponse(answer));
        } catch (Exception e) {
            log.error("Error while processing chat request", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(new ChatResponse("抱歉，服务出现了问题，请稍后再试。"));
        }
    }
}